package com.cloudz.iptv.fragment.footer;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.TypedValue;
import android.view.View;

import com.cloudz.iptv.R;
import com.cloudz.iptv.fragment.BaseFragment;
import com.cloudz.iptv.fragment.content.v1.Page1;
import com.cloudz.iptv.fragment.content.v1.Page2;
import com.cloudz.iptv.fragment.content.v1.Page3;
import com.cloudz.iptv.fragment.content.v1.Page4;
import com.cloudz.iptv.fragment.content.v1.Page5;
import com.cloudz.iptv.fragment.footer.adapter.MenuFooterAdapter;
import com.cloudz.iptv.fragment.footer.adapter.data.ItemMenuFooter;
import com.owen.tvrecyclerview.widget.TvRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @File Name: com.cloudz.iptv.fragment.header
 * @Author: YJ //作者及
 * @Date: 2018-12-12 17:36//完成日期
 * @Description: // 描述
 * @Version: v0.0.1 // 版本信息
 * @Function List: // 主要函数及其功能
 * @Others: // 其它内容的说明
 * @History: // 历史修改记录
 */
public class FooterFragment extends BaseFragment
{


    @BindView(R.id.list_footer)
    TvRecyclerView tvRecyclerViewMenuFooter;

    private Fragment currentFragment = new Fragment();

    private Page1 page1 = new Page1();

    private Page2 page2 = new Page2();

    private Page3 page3 = new Page3();

    private Page4 page4 = new Page4();

    private Page5 page5 = new Page5();

    /**
     * 前一次的page
     */
    private int prePosition = -1;

    @Override
    public int getLayoutId()
    {
        return R.layout.activity_footer;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);

        addListener();

        addAdpater();
    }

    /**
     * 添加监听
     */
    void addListener()
    {

        tvRecyclerViewMenuFooter.setOnItemListener(new TvRecyclerView.OnItemListener()
        {
            // 上次选中
            @Override
            public void onItemPreSelected(TvRecyclerView parent, View itemView, int position)
            {

            }

            // 当前选中
            @Override
            public void onItemSelected(TvRecyclerView parent, View itemView, int position)
            {
                onMoveFocusBorder(itemView, 1f);

                if (prePosition == position)
                {
                    position = -1;
                } else
                {
                    prePosition = position;
                }

                switch (position)
                {
                    // 切换焦点
                    case 0:
                        switchFragment(page2).commit();
                        break;
                    case 1:
                        switchFragment(page1).commit();
                        break;
                    case 2:
                        switchFragment(page3).commit();
                        break;
                    case 3:
                        switchFragment(page4).commit();
                        break;
                    case 4:
                        switchFragment(page5).commit();
                        break;
                    default:
                }


            }

            // 点击
            @Override
            public void onItemClick(TvRecyclerView parent, View itemView, int position)
            {

            }
        });

        // 离开底部菜单
        tvRecyclerViewMenuFooter.setOnFocusChangeListener(new View.OnFocusChangeListener()
        {
            @Override
            public void onFocusChange(View view, boolean b)
            {
//                if (!b)
//                {
//                    focusBorder.setVisible(false);
//                } else
//                {
//                    focusBorder.setVisible(true);
//                }
                focusBorder.setVisible(true);
            }
        });

    }

    /**
     * 添加适配器
     */
    void addAdpater()
    {

        //设置布局横纵间隙
        tvRecyclerViewMenuFooter.setSpacingWithMargins((int) dp2px(20), (int) dp2px(20));

        List<ItemMenuFooter> itemMenuFooters = new ArrayList<>();

        itemMenuFooters.add(new ItemMenuFooter("我的酒店", R.drawable.icon_p1));

        itemMenuFooters.add(new ItemMenuFooter("高清直播", R.drawable.icon_p2));

        itemMenuFooters.add(new ItemMenuFooter("高清影视", R.drawable.icon_p3));

        itemMenuFooters.add(new ItemMenuFooter("酒店服务", R.drawable.icon_p4));

        itemMenuFooters.add(new ItemMenuFooter("团客管理", R.drawable.icon_p5));

        MenuFooterAdapter adapter = new MenuFooterAdapter(getContext(), itemMenuFooters);

        tvRecyclerViewMenuFooter.setAdapter(adapter);
    }

    private float dp2px(int dp)
    {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    /**
     * 切换Fragment
     *
     * @param targetFragment
     * @return
     */
    private FragmentTransaction switchFragment(Fragment targetFragment)
    {

        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        if (!targetFragment.isAdded())
        {
            //第一次使用switchFragment()时currentFragment为null，所以要判断一下
            if (currentFragment != null)
            {
                transaction.hide(currentFragment);
            }
            transaction.add(R.id.content, targetFragment, targetFragment.getClass().getName());

        } else
        {
            transaction.hide(currentFragment).show(targetFragment);
        }

        currentFragment = targetFragment;

        return transaction;

    }

}
